package com.example.ljy;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.StringRes;
import androidx.appcompat.app.AppCompatActivity;

import com.example.ljy.db.UserDao;

public class LoginActivity extends AppCompatActivity {

    private EditText userSelect;
    private EditText userPasswordTv;
    private Button userRegisterBtn;
    private Button userLoginBtn;

    @Override
    protected void onDestroy() {
        super.onDestroy();

    }
    @SuppressLint("WrongViewCast")
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setTheme(R.style.ccAppTheme);
        setContentView(R.layout.activity_login);
        userSelect = findViewById(R.id.loginUser);//选择用户框
        userPasswordTv = findViewById(R.id.password);// 用户密码输入框
        userLoginBtn = findViewById(R.id.userLoginBtn);
        userRegisterBtn = findViewById(R.id.userRegisterBtn);
        userRegisterBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(userSelect.getText().toString()==null || userPasswordTv.getText().toString() == null){
                    //showAlertDialog("输入用户名及密码");

                }else {
                    register(userSelect.getText().toString(),userPasswordTv.getText().toString());

                }
            }
        });
        userLoginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(userSelect.getText().toString()==null || userPasswordTv.getText().toString() == null){
                    //showAlertDialog("输入用户名及密码");
                    //Toast.makeText(this,getString(R.string.tips),Toast.LENGTH_SHORT).show();
                }else {
                    int m = gettimes(userSelect.getText().toString());
                    times(userSelect.getText().toString(),m+1);
                    Login(userSelect.getText().toString(),userPasswordTv.getText().toString(),m);

                   // register(userSelect.getText().toString(),userPasswordTv.getText().toString());

                }
            }
        });
    }
    private void register(String loginUserName,String loginUserPassword){
        //获取SharedPreferences对象
        SharedPreferences sharedPreferences = getSharedPreferences("user",MODE_PRIVATE);
        //获取Editor对象的引用
        SharedPreferences.Editor editor = sharedPreferences.edit();
        //将获取过来的值放入文件
        editor.putString(loginUserName, loginUserPassword);
        //editor.putString(loginUserPassword, loginUserPassword);
        // 提交数据
        editor.commit();
    }
    private void times(String loginUserName,int loginTimes){
        //获取SharedPreferences对象
        SharedPreferences sharedPreferences = getSharedPreferences("times",MODE_PRIVATE);
        //获取Editor对象的引用
        SharedPreferences.Editor editor = sharedPreferences.edit();
        //将获取过来的值放入文件
        editor.putInt(loginUserName, loginTimes);
        //editor.putString(loginUserPassword, loginUserPassword);
        // 提交数据
        editor.commit();
    }
    private void Login(String loginUserName,String loginUserPassword,int m){
        SharedPreferences sharedPreferences= getSharedPreferences("user", MODE_PRIVATE);
        String passWord = sharedPreferences.getString(loginUserName,"");
       if(loginUserPassword.equals(passWord)){
           Intent intent = new Intent(this,MainActivity.class);
           intent.putExtra("times",m);
           startActivity(intent);
           UserDao dao= UserDao.getInstance(this);
           dao.creatDAtabase();
          // SQLiteDatabase dbwriter = dao.getWritableDatabase();//获得数据库写入权限
           finish();
       }

    }
    int gettimes(String loginUserName){
        SharedPreferences sharedPreferences= getSharedPreferences("times", MODE_PRIVATE);
        int m = sharedPreferences.getInt(loginUserName,0);
     return  m;
    }
    //弹出一个对话框
    void showAlertDialog(String msg){
        //Dialog myDialog = new Dialog(this);
        AlertDialog.Builder dialog = new AlertDialog.Builder(this, R.style.MyDialog);

        dialog.setMessage(msg);
        dialog.setCancelable(false);
        dialog.setPositiveButton(getResources().getString(R.string.ok), new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        dialog.show();
    }
//    private void updateUiWithUser(String model) {
//        String welcome = getString(R.string.welcome) + model.getDisplayName();
//        Toast.makeText(getApplicationContext(), welcome, Toast.LENGTH_LONG).show();
//    }

    private void showLoginFailed(@StringRes Integer errorString) {
        Toast.makeText(getApplicationContext(), errorString, Toast.LENGTH_SHORT).show();
    }
}